<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <a-form-model ref="form" :model="model" slot="detail">
        <a-row :gutter="20" style="display: flex;flex-wrap: wrap;">
          <a-col :span="11">
            <a-form-model-item
              label="公司名称"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
              prop="customName"
              :rules="{ required: true, message: '请输入公司名称' }"
            >
              <a-input v-model="model.customName" placeholder="请输入公司名称"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="11">
            <a-form-model-item
              :rules="{ required: true, message: '请输入机构代码' }"
              label="机构代码"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
              prop="code"
            >
              <a-input v-model="model.code" placeholder="请输入机构代码"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="11">
            <a-form-model-item
              :rules="{ required: true, message: '请输入公司地址' }"
              label="公司地址"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
              prop="address"
            >
              <!-- <a-input v-model="model.address" placeholder="请输入公司地址"></a-input> -->
              <a-cascader
                v-model="model.address"
                :options="cityList"
                placeholder="请选择公司地址"
                :fieldNames="{
                  label: 'name',
                  value: 'name',
                  children: 'children'
                }"
              />
            </a-form-model-item>
          </a-col>
          <a-col :span="11">
            <a-form-model-item
              :rules="{ required: true, message: '请输入联系方式' }"
              label="联系方式"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
              prop="contact"
            >
              <a-input v-model="model.contact" placeholder="请输入联系方式"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="11">
            <a-form-model-item
              :rules="{ required: true, message: '请输入年用电量' }"
              label="年用电量"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
              prop="dosage"
            >
              <a-input v-model="model.dosage" placeholder="请输入年用电量"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="11">
            <a-form-model-item
              :rules="{ required: true, message: '请选择签约状态' }"
              label="签约状态"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
              prop="type"
            >
              <j-dict-select-tag type="list" v-model="model.type" dictCode="kh_type" placeholder="请选择签约状态" />
            </a-form-model-item>
          </a-col>
          <a-col :span="11">
            <a-form-model-item
              :rules="{ required: true, message: '请输叙选择开始时间' }"
              label="开始时间"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
              prop="staReportTime"
            >
              <j-date placeholder="请选择开始时间" v-model="model.staReportTime" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="11">
            <a-form-model-item
              :rules="{ required: true, message: '请选择结束时间' }"
              label="结束时间"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
              prop="reportDays"
            >
              <j-date placeholder="请选择结束时间" v-model="model.reportDays" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="11">
            <a-form-model-item
              :rules="{ required: true, message: '请选择类型' }"
              label="类型"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
              prop="userType"
            >
              <j-dict-select-tag type="list" v-model="model.userType" dictCode="bb_type" placeholder="请选择类型" />
            </a-form-model-item>
          </a-col>
          <a-col :span="11">
            <a-form-model-item
              :rules="{ required: true, message: '请输入法人' }"
              label="法人"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
              prop="legalPerson"
            >
              <a-input v-model="model.legalPerson" placeholder="请输入法人"></a-input>
            </a-form-model-item>
          </a-col>
          <!-- <a-col :span="11">
            <a-form-model-item
              :rules="{ required: true, message: '请输入法人' }"
              label="法人"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
              prop="legalPerson"
            >
              <a-input v-model="model.legalPerson" placeholder="请输入法人"></a-input>
            </a-form-model-item>
          </a-col> -->
          <a-col :span="11">
            <a-form-model-item
              :rules="{ required: true, message: '请输入详细地址' }"
              label="详细地址"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
              prop="xxAddress"
            >
              <a-input v-model="model.xxAddress" placeholder="请输入详细地址"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="11">
            <a-form-model-item
              :rules="{ required: true, message: '请输入联系人' }"
              label="联系人"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
              prop="contacts"
            >
              <a-input v-model="model.contacts" placeholder="请输入联系人"></a-input>
            </a-form-model-item>
          </a-col>
        </a-row>
      </a-form-model>
    </j-form-container>
  </a-spin>
</template>

<script>
import { httpAction, getAction } from '@/api/manage'
import { validateDuplicateValue } from '@/utils/util'
import { message } from 'ant-design-vue'
import { apiGetOrganTree } from '@/api/agentManage.js'

export default {
  name: 'GzCustomInfoForm',
  components: {},
  props: {
    //表单禁用
    disabled: {
      type: Boolean,
      default: false,
      required: false
    },
    cityList: {
      type: Array,
      default: () => []
    }
  },
  data() {
    return {
      model: {},
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 }
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 }
      },
      confirmLoading: false,
      validatorRules: {
        phone: [{ required: false }, { pattern: /^1[3456789]\d{9}$/, message: '请输入正确的手机号码!' }]
      },
      url: {
        add: '/gzCustomInfo/add',
        edit: '/gzCustomInfo/edit',
        queryById: '/gzCustomInfo/queryById'
      },
    }
  },
  computed: {
    formDisabled() {
      return this.disabled
    }
  },
  created() {
    //备份model原始值
    this.modelDefault = JSON.parse(JSON.stringify(this.model))
  },
  methods: {
    
    add() {
      this.edit(this.modelDefault)
    },
    edit(record) {
      let formData = Object.assign({}, record)
      formData.address = formData.address.split('-')
      this.model = formData
      console.log(formData, 'formData');
      
      this.visible = true
    },
    submitForm() {
      const that = this
      // 触发表单验证
      this.$refs.form.validate(valid => {
        if (valid) {
          that.confirmLoading = true
          let httpurl = ''
          let method = ''
          if (!this.model.id) {
            httpurl += this.url.add
            method = 'post'
          } else {
            httpurl += this.url.edit
            method = 'put'
          }
          this.model.address = this.model.address.join('-')
          httpAction(httpurl, this.model, method)
            .then(res => {
              if (res.success) {
                that.$message.success(res.message)
                that.$emit('ok')
              } else {
                that.$message.warning(res.message)
              }
            })
            .finally(() => {
              that.confirmLoading = false
            })
        }
      })
    }
  }
}
</script>
